home *** CD-ROM | disk | FTP | other *** search
/ Czech Logic, Card & Gambling Games / Logické hry.iso / hry / Fish Fillets / script / share / level_plan.lua < prev    next >
Text File  |  2005-07-16  |  3KB  |  115 lines

  1.  
  2. -- -----------------------------------------------------------------
  3. -- Random numbers
  4. -- -----------------------------------------------------------------
  5. function random(limit)
  6.     -- return number from [0, limit)
  7.     if limit <= 0 then
  8.         return 0
  9.     else
  10.         return math.random(limit) - 1
  11.     end
  12. end
  13. -- -----------------------------------------------------------------
  14. function randint(start, max)
  15.     -- return number from [start, max]
  16.     if start >= max then
  17.         return start
  18.     else
  19.         return math.random(start, max)
  20.     end
  21. end
  22.  
  23. -- -----------------------------------------------------------------
  24. -- Switch statement for lua
  25. -- -----------------------------------------------------------------
  26. function switch(case)
  27.   return function(codetable)
  28.            local f = codetable[case] or codetable.default
  29.            if f then
  30.              if type(f) == "function" then
  31.                return f(case)
  32.              else
  33.                error("case '"..tostring(case).."' is not a function")
  34.              end
  35.            end
  36.          end
  37. end
  38.  
  39. -- -----------------------------------------------------------------
  40. -- Manipulation with tables
  41. -- -----------------------------------------------------------------
  42. function createArray(size)
  43.     local array = {}
  44.     for i = 0, size - 1 do
  45.         array[i] = 0
  46.     end
  47.     return array
  48. end
  49.  
  50. function isIn(item, table)
  51.     for index, value in pairs(table) do
  52.         if value == item then
  53.             return true
  54.         end
  55.     end
  56.     return false
  57. end
  58.  
  59. function isRange(value, min, max)
  60.     return min <= value and value <= max
  61. end
  62.  
  63. function countPairs(table)
  64.     local count = 0
  65.     for index, value in pairs(table) do
  66.         count = count + 1
  67.     end
  68.     return count
  69. end
  70.  
  71. -- -----------------------------------------------------------------
  72. -- Planning functions
  73. -- -----------------------------------------------------------------
  74. local function isTime(delay, count)
  75.     return count >= delay
  76. end
  77.  
  78. function planTimeAction(delay, action)
  79.     local waitTime = 0
  80.     game_planAction(function(count)
  81.             local done = false
  82.             if dialog_isDialog() then
  83.                 waitTime = count
  84.             elseif isTime(delay + waitTime, count) then
  85.                 action()
  86.                 done = true
  87.             end
  88.             return done
  89.         end)
  90. end
  91.  
  92. function planDialog(actor_index, delay, dialog, action)
  93.     planTimeAction(delay, function()
  94.             model_talk(actor_index, dialog, 100, 0, true)
  95.             if nil ~= action then
  96.             action()
  97.             end
  98.         end)
  99. end
  100.  
  101. -- -----------------------------------------------------------------
  102. -- Options
  103. -- -----------------------------------------------------------------
  104. function optionsGetParam(paramName)
  105.     return options_getParam(paramName)
  106. end
  107. function optionsGetAsInt(paramName)
  108.     local value = tonumber(options_getParam(paramName))
  109.     if nil == value then
  110.         value = 0
  111.     end
  112.     return value
  113. end
  114.  
  115.